﻿using Masuit.Tools.Security;
using Microsoft.EntityFrameworkCore;

namespace Canteen.Core.Models
{
    public class CanteenDbContext:DbContext
    {
        public const string SecretKey = "COmHYlfGA7P82EaI";//16位密钥，不能修改
        public CanteenDbContext(DbContextOptions<CanteenDbContext> options) : base(options) { }

        public DbSet<User> Users { get; set; }
        public DbSet<Store> Stores { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ShopOrder> ShopOrders { get; set; }
        public DbSet<ShopOrderProducts> ShopOrderProducts { get; set; }
        public DbSet<RechargeRecord> RechargeRecords { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasIndex(e => e.Phone).HasFilter("[Phone] IS NOT NULL");//添加索引
                entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");//设置默认值

                entity.Property(e => e.IdCard).HasConversion(//转换身份证号
                    v => string.IsNullOrWhiteSpace(v) ? null : v.AESEncrypt(SecretKey),//AES密钥加密为密文
                    v => string.IsNullOrEmpty(v) ? null : v.AESDecrypt(SecretKey)//AES密钥解密为明文
                );
                entity.Property(e => e.Phone).HasConversion(//转换手机号码
                    v => string.IsNullOrWhiteSpace(v) ? null : v.AESEncrypt(SecretKey),//AES密钥加密为密文
                    v => string.IsNullOrEmpty(v) ? null : v.AESDecrypt(SecretKey)//AES密钥解密为明文
                );
                entity.Property(e => e.Email).HasConversion(//转换邮箱
                    v => string.IsNullOrWhiteSpace(v) ? null : v.AESEncrypt(SecretKey),//AES密钥加密为密文
                    v => string.IsNullOrEmpty(v) ? null : v.AESDecrypt(SecretKey)//AES密钥解密为明文
                );
                entity.Property(e => e.Account).HasConversion(//转换账号
                    v => string.IsNullOrWhiteSpace(v) ? null : v.AESEncrypt(SecretKey),//AES密钥加密为密文
                    v => string.IsNullOrEmpty(v) ? null : v.AESDecrypt(SecretKey)//AES密钥解密为明文
                );
                entity.Property(e => e.PassWord).HasConversion(//转换登录密码
                    v => string.IsNullOrEmpty(v) ? null : v.MDString2(SecretKey),//密码转换MD5
                    v => v
                );
            });
            modelBuilder.Entity<Store>(entity => {
                entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");//设置默认值
                entity.Property(e => e.Phone).HasConversion(//转换手机号码
                    v => string.IsNullOrWhiteSpace(v) ? null : v.AESEncrypt(SecretKey),//AES密钥加密为密文
                    v => string.IsNullOrEmpty(v) ? null : v.AESDecrypt(SecretKey)//AES密钥解密为明文
                );
                entity.Property(e => e.PassWord).HasConversion(//转换登录密码
                   v => string.IsNullOrEmpty(v) ? null : v.MDString2(SecretKey),//密码转换MD5
                   v => v
               );
            });
            modelBuilder.Entity<Product>(entity => {
                entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");//设置默认值
            });
            modelBuilder.Entity<ShopOrder>(entity => {
                entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");//设置默认值
            });
            modelBuilder.Entity<ShopOrderProducts>(entity =>
            {
                entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");//设置默认值
            });
            modelBuilder.Entity<ShopOrderProducts>()
                .HasOne(x => x.ShopOrder)
                .WithMany(x => x.ShopOrderProductList)
                .HasForeignKey(x => x.ShopOrderId)
                .OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<ShopOrderProducts>()
                .HasOne(x => x.Product)
                .WithMany(x => x.ShopOrderProductList)
                .HasForeignKey(x => x.ProductId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<RechargeRecord>(entity => {
                entity.Property(e => e.CreateTime).HasDefaultValueSql("GETDATE()");//设置默认值
            });
        }
    }
}
